
BuilderDesigner = function(){

    this.create = function(json){
    
        var nodeRifer = document.getElementById("schema"); // Deve passar via parametro o node de referencia
        var i = 0;
        var html = "";
        while (json.main[i] != null) {
            var key, value;
            var newdiv = document.createElement(json.main[i].layout[0].tag);
            newdiv.setAttribute("id", json.main[i].layout[0].id);
            
            try {
                for (var x = 0; x < json.main[i].layout[0].style.length; x++) {
                    txtStyle = json.main[i].layout[0].style[x];
                    key = txtStyle.substring(0, txtStyle.indexOf(":"));
                    value = txtStyle.substring(txtStyle.indexOf(":") + 1, txtStyle.length);
                    newdiv.style[key] = value;
                }
            } 
            catch (ex) {
            }
            
            /*                if (nodeRifer.width.replace("px", "") != "") 
             newdiv.style.width = (nodeRifer.style.width.replace("px", "") + 10) + "px";
             */
            try {
                for (var x = 0; x < json.main[i].layout[0].params.length; x++) {
                    txtParams = json.main[i].layout[0].params[x];
                    key = txtParams.substring(0, txtParams.indexOf(":"));
                    value = txtParams.substring(txtParams.indexOf(":") + 1, txtParams.length);
                    newdiv.style[key] = value;
                    //					log("txtStyle = " + txtParams);
                }
            } 
            catch (ex) {
            }
            try {
				var events = json.main[i].layout[0].events[0];
                newdiv["onmousedown"] = (events.onmousedown!=null) ? events.onmousedown : null;
                newdiv["onmouseup"]   = (events.onmouseup!=null)   ? events.onmouseup   : null;
                newdiv["onmousemove"] = (events.onmousemove!=null) ? events.onmousemove : null;
                newdiv["onmouseout"]  = (events.onmouseout!=null)  ? events.onmouseout  : null;
                newdiv["onclick"] 	  = (events.onclick!=null)     ? events.onclick     : null;
            } 
            catch (ex) {
            }
            
            newdiv.innerHTML = json.main[i].layout[0].innerHTML;
            document.getElementById(json.main[i].layout[0].nodeRifer).appendChild(newdiv);
            i++;
        }
    }
    
}

Layout = function(){
    this.renderize = function(region, resize, fixedmodify, menu, statusbar){
    
        try {
        
            document.getElementById(resize).style.left = (document.getElementById(region).style.width.replace('px', '') - 15) + "px";
            document.getElementById(resize).style.top = (document.getElementById(region).style.height.replace('px', '') - 0) + "px";
            
            document.getElementById(fixedmodify).style.left = (document.getElementById(region).style.width.replace('px', '') - 25) + "px";
            document.getElementById(menu).style.width = document.getElementById(region).style.width;
            
            document.getElementById(statusbar).style.width = document.getElementById(region).style.width;
            document.getElementById(statusbar).style.top = document.getElementById(region).style.height;
        } 
        catch (ex) {
        }
        
    }
}

// Insere cada div criado em um mapa com a chave "div.id" 
JsonBind = function(){
	var comma = "";
	var elements = new Map();
	var txtJson = "{\"main\": [";
	
	this.addElements = function(jsonSchema){
		elements.put(jsonSchema.id, jsonSchema);
//		alert("jsonSchema.id = " + jsonSchema.id);
	}
	
	//Return string no formato Json
	this.toString = function(){
		for(var i=0; i<elements.size(); i++){
			jsonSchema = elements.elementAt(i);
			generateJson(jsonSchema);
		}
		return  txtJson += "]};" ;
	}
	
	function generateJson(json){
		txtJson += comma + "{ \"layout\": [{";
		txtJson += "\"tag\":\""+json.tag+"\"," + "\"id\":\""+json.id+"\",";
		txtJson += "\"params\":[";		
		if(json.param != null)
			txtJson += json.param.toString();

		txtJson += "],";
		txtJson += "\"nodeRifer\":\"" + json.nodeRifer + "\",";
		txtJson += "\"innerHTML\":\"" + json.innerHTML + "\",";
		txtJson += "\"style\":[" + json.style.toString() + "],";

		txtJson += "\"events\":[{";		
		if(json.events != null)
			txtJson += json.events.toString("str/obj");
		txtJson += "}]";

//		document.getElementById("result").text = json.events.toString();
		txtJson += "}]}";
		comma = ",";
	}		
	
}

//A estrutura do Json para cada div a ser criado
JsonSchema = function(){

	this.tag = "";
	this.id = "";
	this.params = new Map();
	this.nodeRifer = "";
	this.innerHTML = "";
	this.style = new Map();
	this.events = new Map();
	

}
// Simula a funcionalidade do HashMap em Java
Map = function() {
	var keys = new Array();
	var values = new Array();
	this.get = function(key){ return keys[keys.indexOf(key)]; }		
	this.elementAt = function(index){ return values[index]; }		
	this.listKeys = function(){ return keys; }		
	this.put = function(key, value){
		var index = keys.indexOf(key);

		if (index >= 0) {
			values[index] = value;
			return;
		}
		keys[keys.length] = key;
		values[values.length] = value;
	}
	this.size = function(){ return keys.length; }

	// tipo de formato: "str/str", "str/obj"
	this.toString = function(format){ 
		var delim="";
		var txt=""; 
		for(var i=0; i < keys.length; i++){
			if(format == "str/obj")
				txt += delim +"\""+ keys[i] + "\":" + values[i];
			else
				txt += delim +"\""+ keys[i] + ":" + values[i]+"\"";
					
			delim=",";
		}
		return txt;
	}
	
}

function teste(){
	bind = new JsonBind();

	jsonSchema = new JsonSchema();
	jsonSchema.tag = "div";
	jsonSchema.id = "logo";
	jsonSchema.nodeRifer = "schema";
	jsonSchema.innerHTML = "<img src='logo.gif' id='_logoimg' align='bottom'/>";

	jsonSchema.style.put("top","8px");
	jsonSchema.style.put("width","100px");
	jsonSchema.style.put("position","absolute");
	jsonSchema.style.put("backgroundColor","#f0f2f4");
	bind.addElements(jsonSchema);

	// Cria Menu-Bar default
	jsonSchema = new JsonSchema();
	jsonSchema.tag = "div";
	jsonSchema.id = "menubar";
	jsonSchema.nodeRifer = "logo";
	
	jsonSchema.style.put("top","-10px");
	jsonSchema.style.put("left","0px");
	jsonSchema.style.put("height","20px");
	jsonSchema.style.put("width","100px");
	jsonSchema.style.put("backgroundImage","url(img/white-top-bottom.gif)");
	
	jsonSchema.events.put("onmousedown", "function(){" +
											" dragdrop.addEvent(document.getElementById('logo'), 'move'); " +
											" document.getElementById('_fixmodify').src = 'img/fix_modify_enable.gif'; " +
										 "}"
	);
	bind.addElements(jsonSchema);
			

	// Cria Icon Fix default
	jsonSchema = new JsonSchema();
	jsonSchema.tag = "div";
	jsonSchema.id = "fixmodify";
	jsonSchema.nodeRifer = "logo";
	jsonSchema.innerHTML = "<img id='_fixmodify' src='img/fix_modify_enable.gif' width='14' height='16'/>";
	
	jsonSchema.style.put("position","absolute");
	jsonSchema.style.put("top","-10px");
	jsonSchema.style.put("left","79px");

	jsonSchema.events.put("onmousedown", "function(){" +
											" 	document.getElementById('_fixmodify').src='img/fix_modify_disable.gif'; " +
										 "}"
	);
	bind.addElements(jsonSchema);

	// Cria Caption default
	jsonSchema = new JsonSchema();
	jsonSchema.tag = "div";
	jsonSchema.id = "tittle";
	jsonSchema.nodeRifer = "logo";
	jsonSchema.innerHTML = "<span id='title' class='title-blue'>logo</span>";
	
	jsonSchema.style.put("position","absolute");
	jsonSchema.style.put("top","-8px");
	jsonSchema.style.put("left","10px");
	
	bind.addElements(jsonSchema);


 	//Cria StatusBar default
 	jsonSchema = new JsonSchema();
	jsonSchema.tag = "div";
	jsonSchema.id = "statusbar";
	jsonSchema.nodeRifer = "logo";
	
	jsonSchema.style.put("top","95px");
	jsonSchema.style.put("left","0px");
	jsonSchema.style.put("height","15px");
	jsonSchema.style.put("width","96px");
	jsonSchema.style.put("backgroundImage","url(img/white-top-bottom.gif)");
	
	jsonSchema.events.put("onmousedown", "function(){" +
											" dragdrop.addEvent(document.getElementById('logo'), 'resize', buildDesign.renderize, ['logo', 'resize', 'fixmodify', 'menubar', 'statusbar']); " +
										 "}"
	);

	bind.addElements(jsonSchema);
	
	//Cria Image de resize default
	jsonSchema = new JsonSchema();
	jsonSchema.tag = "div";
	jsonSchema.id = "resize";
	jsonSchema.nodeRifer = "logo";
	jsonSchema.innerHTML = "<img id='resize' src='img/resizediv.gif'/>";
	
	jsonSchema.style.put("position","absolute");
	jsonSchema.style.put("top","95px");
	jsonSchema.style.put("left","80px");
	
	bind.addElements(jsonSchema);

	return eval("json = " + bind.toString());

}

	





//class Layout extends BuilderDesigner
Layout.prototype = new BuilderDesigner;
buildDesign = new Layout;




